<?php
use yii\bootstrap\ActiveForm;
use yii\helpers\Url;

?>
<div class="login-box">
    <div class="login-logo">
        <a href="<?= Url::toRoute('site/login') ?>"><?= yii::$app->params["appName"] ?></a>
    </div>
    <!-- /.login-logo -->
    <div class="login-box-body">
        <div class="layui-tab" lay-filter="switchtab">
            <ul class="layui-tab-title">
                <li class="layui-this">登录</li>
                <li>注册</li>
            </ul>
            <div class="layui-tab-content">
                <div class="layui-tab-item layui-show">
                    <!--登录选项卡-->
                    <p class="login-box-msg">登录平台账号?</p>
                    <?php $form = ActiveForm::begin(['id' => 'login-form', 'class' => 'layui-form', 'action' => Url::toRoute('site/login')]); ?>
                    <div class="form-group has-feedback">
                        <input name="username" id="username" type="text" lay-verify="username" autocomplete="off" class="form-control" placeholder="用户名"/>
                        <span class="glyphicon glyphicon-envelope form-control-feedback"></span>
                    </div>
                    <div class="form-group has-feedback">
                        <input name="password" id="password" type="password" lay-verify="password" autocomplete="off" class="form-control" placeholder="密码">
                        <span class="glyphicon glyphicon-lock form-control-feedback"></span>
                    </div>
                    <div class="row">
                        <div class="col-xs-8">
                            <div class="checkbox icheck">
                                <label>
                                    <input name="remember" id="remember" value="y" type="checkbox"/> &nbsp;记住我的登录
                                </label>
                            </div>
                        </div>
                        <!-- /.col -->
                        <div class="layui-form-item">
                            <button id="login_btn" type="button" lay-submit class="layui-btn" lay-filter="login_btn">
                                登录
                            </button>
                            <button type="reset" class="layui-btn layui-btn-primary login-reset">重置</button>
                        </div>
                        <!-- /.col -->
                    </div>
                    <!-- </form>  -->
                    <?php ActiveForm::end(); ?>


                </div>
                <div class="layui-tab-item">
                    <!--注册选项卡-->
                    <form class="layui-form" id="regesitbox" action="" method="post">
                        <div class="layui-inline">
                            <label class="layui-form-label">邮箱</label>
                            <div class="layui-input-inline">
                                <input type="text" name="email" lay-verify="email" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">密码</label>
                            <div class="layui-input-inline">
                                <input type="password" name="password" required lay-verify="pass" placeholder="请输入密码" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">确认</label>
                            <div class="layui-input-inline">
                                <input type="password" name="repassword" required lay-verify="pass" placeholder="请再次输入密码" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <div class="layui-input-block">
                                <button class="layui-btn" lay-submit lay-filter="regesitnow">立即提交</button>
                                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                            </div>
                        </div>
                    </form>

                    <script>
                        //Demo

                        layui.use('form', function(){
                            var form = layui.form;
                            //监听提交
                            form.on('submit(regesitnow)', function(data){
                                var formdata=JSON.stringify(data.field);
                                var csrfToken = $('meta[name="csrf-token"]').attr("content");
                                $.post("/site/regesit",{"data":formdata,"_csrf":csrfToken},function (res) {
                                   if(res.code>0)
                                       layer.msg(res.messages,function () {
                                           location.href=res.url;
                                       });
                                   else
                                       layer.msg(res.messages);
                                },"json");
                                return false;
                            });
                            form.verify({
                                pass: [
                                    /^[\S]{6,12}$/
                                    ,'密码必须6到12位，且不能出现空格'
                                ]
                            });
                        });
                    </script>



                </div>
            </div>
        </div>
    </div>
    <!-- /.login-box-body -->
</div>
<!-- /.login-box -->

<script>
//    $(document).on('keydown', function (e) {  //document为当前元素，限制范围，如果不限制的话会一直有事件
//        if (e.keyCode == 13) {
//            $('#login-form').submit();
//        }
//    });
    $('#login_btn').click(function (e) {
        e.preventDefault();
        $('#login-form').submit();
    });
    $('#login-form').bind('submit', function (e) {
        e.preventDefault();
        $(this).ajaxSubmit({
            type: "post",
            dataType: "json",
            url: "<?=Url::toRoute('site/login')?>",
            success: function (value) {
                if (value.errno == 0) {
                    location.href = value.url;
                }
                else {
                    $('#username').attr({
                        'data-placement': 'top',
                        'data-content': '<span class="text-danger">用户名或密码错误</span>',
                        'data-toggle': 'popover'
                    }).addClass('popover-show').popover({html: true}).popover('show');
                }

            }
        });
    });
</script>
<script>
    //注意：选项卡 依赖 element 模块，否则无法进行功能性操作
    layui.use('element', function(){
        var element = layui.element;
    });
</script>